

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>agpy.regfile_aperture_extract &mdash; agpy 0.1.2 documentation</title>
    
    <link rel="stylesheet" href="../../_static/extra.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.1.2',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="agpy 0.1.2 documentation" href="../../index.html" />
    <link rel="up" title="agpy" href="../agpy.html" />
     
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setDomainName', 'pyspeckit.bitbucket.org']);
      _gaq.push(['_setAllowHash', false]);
      _gaq.push(['_trackPageview']);


    </script>
    <link rel="stylesheet" type="text/css" href="../../_static/extra.css" />
  </head>
  <body>
    <div class="header-wrapper">
      <div class="header">
        <h1><a href="../../index.html">agpy 0.1.2 documentation</a></h1>
        <div class="rel">
          <a href="http://agpy.googlecode.com">agpy Home </a>  |
          <a href=../../index.html>Docs Home </a>  |
          <a href="http://code.google.com/p/agpy/w/list">Wiki</a>  |
          <a href=../../search.html>Search </a>
        </div>
       </div>
    </div>

    <div class="content-wrapper">
      <div class="content">
        <div class="document">
            
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <h1>Source code for agpy.regfile_aperture_extract</h1><div class="highlight"><pre>
<span class="kn">import</span> <span class="nn">pyregion</span>
<span class="kn">import</span> <span class="nn">pyfits</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">pywcs</span>
<span class="kn">from</span> <span class="nn">agpy.region_positions</span> <span class="kn">import</span> <span class="o">*</span>

<div class="viewcode-block" id="get_fluxes"><a class="viewcode-back" href="../../agpy.html#agpy.regfile_aperture_extract.get_fluxes">[docs]</a><span class="k">def</span> <span class="nf">get_fluxes</span><span class="p">(</span><span class="n">regfile</span><span class="p">,</span> <span class="n">outfile</span><span class="p">,</span> <span class="n">inneraprad</span><span class="o">=</span><span class="mi">35</span><span class="p">,</span> <span class="n">outeraprad</span><span class="o">=</span><span class="mi">60</span><span class="p">,</span> <span class="n">hdu</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">PPBEAM</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span> <span class="n">debug</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">print_nulls</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Extract fluxes from a region-defined aperture with inner and outer circular apertures</span>
<span class="sd">    specififed</span>

<span class="sd">    MUST BE IN GALACTIC COORDINATES</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">if</span> <span class="n">hdu</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;hdu keyword is required&quot;</span><span class="p">)</span>

    <span class="n">data</span> <span class="o">=</span> <span class="n">hdu</span><span class="o">.</span><span class="n">data</span>
    <span class="n">header</span> <span class="o">=</span> <span class="n">hdu</span><span class="o">.</span><span class="n">header</span>
    <span class="n">wcs</span> <span class="o">=</span> <span class="n">pywcs</span><span class="o">.</span><span class="n">WCS</span><span class="p">(</span><span class="n">header</span><span class="p">)</span>
    <span class="n">glonmax</span> <span class="o">=</span> <span class="n">wcs</span><span class="o">.</span><span class="n">wcs_pix2sky</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
    <span class="n">glonmin</span> <span class="o">=</span> <span class="n">wcs</span><span class="o">.</span><span class="n">wcs_pix2sky</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">)[</span><span class="mi">1</span><span class="p">]</span>

    <span class="n">reglist</span> <span class="o">=</span> <span class="n">pyregion</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">regfile</span><span class="p">)</span>

    <span class="n">outf</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">outfile</span><span class="p">,</span><span class="s">&#39;w&#39;</span><span class="p">)</span>

    <span class="k">print</span> <span class="s">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%16s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">s</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;Source_Name&#39;</span><span class="p">,</span><span class="s">&#39;SumJy&#39;</span><span class="p">,</span><span class="s">&#39;ApSumJy&#39;</span><span class="p">,</span><span class="s">&#39;MeanApSumJy&#39;</span><span class="p">,</span><span class="s">&#39;SumJyBm&#39;</span><span class="p">,</span><span class="s">&#39;ApSumJyBm&#39;</span><span class="p">,</span><span class="s">&#39;BgMed&#39;</span><span class="p">,</span><span class="s">&#39;BgMean&#39;</span><span class="p">,</span><span class="s">&#39;BgStd&#39;</span><span class="p">,</span><span class="s">&#39;FracErrBg&#39;</span><span class="p">])</span>
    <span class="k">print</span> <span class="o">&gt;&gt;</span><span class="n">outf</span><span class="p">,</span><span class="s">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%16s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">s</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;Source_Name&#39;</span><span class="p">,</span><span class="s">&#39;SumJy&#39;</span><span class="p">,</span><span class="s">&#39;ApSumJy&#39;</span><span class="p">,</span><span class="s">&#39;MeanApSumJy&#39;</span><span class="p">,</span><span class="s">&#39;SumJyBm&#39;</span><span class="p">,</span><span class="s">&#39;ApSumJyBm&#39;</span><span class="p">,</span><span class="s">&#39;BgMed&#39;</span><span class="p">,</span><span class="s">&#39;BgMean&#39;</span><span class="p">,</span><span class="s">&#39;BgStd&#39;</span><span class="p">,</span><span class="s">&#39;FracErrBg&#39;</span><span class="p">])</span>

    <span class="k">for</span> <span class="n">reg</span> <span class="ow">in</span> <span class="n">reglist</span><span class="p">:</span>
        <span class="n">glon</span><span class="p">,</span><span class="n">glat</span> <span class="o">=</span> <span class="n">position_region</span><span class="p">(</span><span class="n">reg</span><span class="p">)</span><span class="o">.</span><span class="n">galactic</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span><span class="p">((</span><span class="n">glon</span> <span class="o">&gt;</span> <span class="n">glonmin</span><span class="p">)</span> <span class="ow">and</span> <span class="p">(</span><span class="n">glon</span> <span class="o">&lt;</span> <span class="n">glonmax</span><span class="p">)):</span>
            <span class="c"># these are the limits of the survey</span>
            <span class="k">if</span> <span class="n">print_nulls</span><span class="p">:</span>
                <span class="k">print</span> <span class="o">&gt;&gt;</span><span class="n">outf</span><span class="p">,</span><span class="s">&quot;</span><span class="si">%16s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">sourcename</span><span class="p">,</span><span class="s">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%16s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">f</span> 
                        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">])</span>
            <span class="k">continue</span>
        <span class="n">xc</span><span class="p">,</span><span class="n">yc</span> <span class="o">=</span> <span class="n">wcs</span><span class="o">.</span><span class="n">wcs_sky2pix</span><span class="p">(</span><span class="n">glon</span><span class="p">,</span><span class="n">glat</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">xc</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">xc</span> <span class="o">&gt;</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="ow">or</span> <span class="n">yc</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">yc</span> <span class="o">&gt;</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span>
            <span class="k">if</span> <span class="n">print_nulls</span><span class="p">:</span>
                <span class="k">print</span> <span class="o">&gt;&gt;</span><span class="n">outf</span><span class="p">,</span><span class="s">&quot;</span><span class="si">%16s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">sourcename</span><span class="p">,</span><span class="s">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%16s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">f</span> 
                        <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">,</span><span class="s">&#39;-&#39;</span><span class="p">])</span>
            <span class="k">continue</span>
        <span class="n">regL</span> <span class="o">=</span> <span class="n">pyregion</span><span class="o">.</span><span class="n">ShapeList</span><span class="p">()</span>
        <span class="n">reg</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="s">&#39;circle&#39;</span>
        <span class="k">while</span> <span class="nb">len</span><span class="p">(</span><span class="n">reg</span><span class="o">.</span><span class="n">coord_list</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="p">:</span>
            <span class="n">reg</span><span class="o">.</span><span class="n">coord_list</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
        <span class="n">reg</span><span class="o">.</span><span class="n">coord_list</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">inneraprad</span><span class="o">/</span><span class="mf">3600.0</span>  <span class="c"># set inner aperture (foreground) to R=25&quot;</span>
        <span class="n">regL</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reg</span><span class="p">)</span>
        <span class="n">innerap</span> <span class="o">=</span> <span class="n">regL</span><span class="o">.</span><span class="n">get_mask</span><span class="p">(</span><span class="n">hdu</span><span class="o">=</span><span class="n">hdu</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">innerap</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;Skipped a source that was in the boundaries: &quot;</span><span class="p">,</span><span class="n">reg</span>
            <span class="k">continue</span>
        <span class="n">regL</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">coord_list</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="n">outeraprad</span><span class="o">/</span><span class="mf">3600.0</span>  <span class="c"># set outer aperture (background) to R=100&quot;</span>
        <span class="c">#regL.append(reg) # I think this turns two circles into a panda?</span>
        <span class="n">outerap</span> <span class="o">=</span> <span class="n">regL</span><span class="o">.</span><span class="n">get_mask</span><span class="p">(</span><span class="n">hdu</span><span class="o">=</span><span class="n">hdu</span><span class="p">)</span>
        <span class="n">backreg</span> <span class="o">=</span> <span class="n">outerap</span><span class="o">-</span><span class="n">innerap</span>

        <span class="n">total</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">innerap</span><span class="p">]</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
        <span class="n">background</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">median</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="n">backreg</span><span class="p">])</span>
        <span class="n">backmean</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">backreg</span><span class="p">]</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span>
        <span class="n">backstd</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">backreg</span><span class="p">]</span><span class="o">.</span><span class="n">std</span><span class="p">()</span>

        <span class="n">sourcename</span> <span class="o">=</span> <span class="n">pos_to_name</span><span class="p">(</span><span class="n">reg</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">backstd</span> <span class="o">&gt;</span> <span class="n">total</span> <span class="ow">or</span> <span class="n">total</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">print</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> set to zero&quot;</span> <span class="o">%</span> <span class="n">reg</span><span class="o">.</span><span class="n">attr</span><span class="p">[</span><span class="mi">1</span><span class="p">][</span><span class="s">&#39;text&#39;</span><span class="p">]</span>
            <span class="n">total</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="n">total_backsub</span>  <span class="o">=</span> <span class="mi">0</span>
            <span class="n">total_mbacksub</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">total_backsub</span>  <span class="o">=</span> <span class="n">total</span> <span class="o">-</span> <span class="n">innerap</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span> <span class="o">*</span> <span class="n">background</span>
            <span class="n">total_mbacksub</span> <span class="o">=</span> <span class="n">total</span> <span class="o">-</span> <span class="n">innerap</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span> <span class="o">*</span> <span class="n">backmean</span>

        <span class="k">print</span> <span class="s">&quot;</span><span class="si">%16s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">sourcename</span><span class="p">,</span><span class="s">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%16.5g</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">f</span> 
                <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="p">[</span><span class="n">total</span><span class="o">/</span><span class="n">PPBEAM</span><span class="p">,</span><span class="n">total_backsub</span><span class="o">/</span><span class="n">PPBEAM</span><span class="p">,</span><span class="n">total_mbacksub</span><span class="o">/</span><span class="n">PPBEAM</span><span class="p">,</span><span class="n">total</span><span class="p">,</span><span class="n">total_backsub</span><span class="p">,</span><span class="n">background</span><span class="p">,</span><span class="n">backmean</span><span class="p">,</span><span class="n">backstd</span><span class="p">,</span><span class="n">backstd</span><span class="o">/</span><span class="n">total_backsub</span><span class="p">])</span>
        <span class="k">print</span> <span class="o">&gt;&gt;</span><span class="n">outf</span><span class="p">,</span><span class="s">&quot;</span><span class="si">%16s</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">sourcename</span><span class="p">,</span><span class="s">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%16.5g</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">f</span> 
                <span class="k">for</span> <span class="n">f</span> <span class="ow">in</span> <span class="p">[</span><span class="n">total</span><span class="o">/</span><span class="n">PPBEAM</span><span class="p">,</span><span class="n">total_backsub</span><span class="o">/</span><span class="n">PPBEAM</span><span class="p">,</span><span class="n">total_mbacksub</span><span class="o">/</span><span class="n">PPBEAM</span><span class="p">,</span><span class="n">total</span><span class="p">,</span><span class="n">total_backsub</span><span class="p">,</span><span class="n">background</span><span class="p">,</span><span class="n">backmean</span><span class="p">,</span><span class="n">backstd</span><span class="p">,</span><span class="n">backstd</span><span class="o">/</span><span class="n">total_backsub</span><span class="p">])</span>

    <span class="n">outf</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div></div>

          </div>
        </div>
      </div>
        </div>
        <div class="sidebar">
          <h3>Table Of Contents</h3>
          <ul>
<li class="toctree-l1"><a class="reference internal" href="../../agpy.html">Adam Ginsburg&#8217;s Python Code (agpy)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../image_tools.html">Image Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fft_tools.html">AG_fft_tools Package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../plfit.html">plfit Package</a></li>
</ul>

          <h3 style="margin-top: 1.5em;">Search</h3>
          <form class="search" action="../../search.html" method="get">
            <input type="text" name="q" />
            <input type="submit" value="Go" />
            <input type="hidden" name="check_keywords" value="yes" />
            <input type="hidden" name="area" value="default" />
          </form>
          <p class="searchtip" style="font-size: 90%">
            Enter search terms or a module, class or function name.
          </p>
        </div>
        <div class="clearer"></div>
      </div>
    </div>

    <div class="footer">
      &copy; Copyright 2011, Adam Ginsburg.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2pre.
    <script type="text/javascript">

      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-6253248-2']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();

    </script>
        
    </div>
  </body>
</html>